libkovan  1
The kovan standard library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
camera.hpp
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright 2012 KISS Institute for Practical Robotics *
3  * *
4  * This file is part of libkovan. *
5  * *
6  * libkovan is free software: you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation, either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * libkovan is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with libkovan. Check the LICENSE file in the project root. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  **************************************************************************/
20 
21 #ifndef _CAMERA_HPP_
22 #define _CAMERA_HPP_
23 
24 #include "geom.hpp"
25 #include "color.hpp"
26 #include "config.hpp"
27 #include "export.h"
28 #include <cstring>
29 #include <string>
30 #include <vector>
31 #include <map>
32 #include <iostream>
33 
34 #ifndef WIN32
35 #include <sys/time.h>
36 #else
37 #define NOMINMAX
38 #include <time.h>
39 #include <winsock2.h>
40 #endif
41 
42 #include <opencv2/core/core.hpp>
43 
44 // These keys are used in the config files loaded by
45 // Camera::Device
46 #define CAMERA_GROUP ("camera")
47 #define CAMERA_NUM_CHANNELS_KEY ("num_channels")
48 #define CAMERA_CHANNEL_GROUP_PREFIX ("channel_")
49 #define CAMERA_CHANNEL_TYPE_KEY ("type")
50 
51 #define CAMERA_CHANNEL_TYPE_HSV_KEY ("hsv")
52 #define CAMERA_CHANNEL_TYPE_QR_KEY ("qr")
53 
54 namespace cv
55 {
56  class VideoCapture;
57 }
58 
59 namespace Camera
60 {
61  class Device;
62 
64  {
65  public:
66  Object(const Point2<unsigned> &centroid,
67  const Rect<unsigned> &boundingBox,
68  const double &confidence, const char *data = 0,
69  const size_t &dataLength = 0);
70  Object(const Object &rhs);
71  ~Object();
72 
73  const Point2<unsigned> &centroid() const;
74  const Rect<unsigned> &boundingBox() const;
75  const double confidence() const;
76  const char *data() const;
77  const size_t dataLength() const;
78 
79  private:
80  Point2<unsigned> m_centroid;
81  Rect<unsigned> m_boundingBox;
82  double m_confidence;
83  char *m_data;
84  size_t m_dataLength;
85  };
86 
87  typedef std::vector<Object> ObjectVector;
88 
90  {
91  public:
92  ChannelImpl();
93  virtual ~ChannelImpl();
94 
95  void setImage(const cv::Mat &image);
96  ObjectVector objects(const Config &config);
97 
98  protected:
99  virtual void update(const cv::Mat &image) = 0;
100  virtual ObjectVector findObjects(const Config &config) = 0;
101 
102  private:
103  bool m_dirty;
104  cv::Mat m_image;
105  };
106 
108  {
109  public:
110  virtual ~ChannelImplManager();
111  virtual void setImage(const cv::Mat &image) = 0;
112  virtual ChannelImpl *channelImpl(const std::string &name) = 0;
113  };
114 
116  {
117  public:
120 
121  virtual void setImage(const cv::Mat &image);
122  virtual ChannelImpl *channelImpl(const std::string &name);
123 
124  private:
125  std::map<std::string, ChannelImpl *> m_channelImpls;
126  };
127 
129  {
130  public:
131  Channel(Device *device, const Config &config);
132  ~Channel();
133 
134  void invalidate();
135 
136  const ObjectVector *objects() const;
137 
138  Device *device() const;
139 
143  void setConfig(const Config &config);
144 
145  private:
146  Device *m_device;
147  Config m_config;
148  mutable ObjectVector m_objects;
149  ChannelImpl *m_impl;
150  mutable bool m_valid;
151  };
152 
153  typedef std::vector<Channel *> ChannelPtrVector;
154 
156  {
157  public:
158  static std::string extension();
159 
160  static void setBasePath(const std::string &path);
161  static std::string path(const std::string &name = std::string());
162  static std::string defaultPath();
163  static std::string defaultConfigPath();
164  static void setDefaultConfigPath(const std::string &name);
165 
166  private:
167  static std::string s_path;
168  };
169 
171  {
172  public:
173  virtual ~InputProvider();
174  virtual bool open(const int number) = 0;
175  virtual bool isOpen() const = 0;
176  virtual void setWidth(const unsigned width) = 0;
177  virtual void setHeight(const unsigned height) = 0;
178  virtual bool next(cv::Mat &image) = 0;
179  virtual bool close() = 0;
180  };
181 
183  {
184  public:
186  ~UsbInputProvider();
187 
188  virtual bool open(const int number);
189  virtual bool isOpen() const;
190  virtual void setWidth(const unsigned width);
191  virtual void setHeight(const unsigned height);
192  virtual bool next(cv::Mat &image);
193  virtual bool close();
194 
195  private:
196  cv::VideoCapture *m_capture;
197  };
198 
200  {
201  public:
202  Device(InputProvider *const inputProvider);
203  ~Device();
204 
205  bool open(const int number = 0);
206  bool isOpen() const;
207  bool close();
208  bool update();
209 
210  void setWidth(const unsigned width);
211  void setHeight(const unsigned height);
212 
213  unsigned width() const;
214  unsigned height() const;
215 
216  const ChannelPtrVector &channels() const;
217 
218  InputProvider *inputProvider() const;
219  const cv::Mat &rawImage() const;
220 
221  void setConfig(const Config &config);
222  const Config &config() const;
223 
224  void setChannelImplManager(ChannelImplManager *channelImplManager);
225  ChannelImplManager *channelImplManager() const;
226 
227  const unsigned char *bgr() const;
228 
229  private:
230  void updateConfig();
231 
232  InputProvider *const m_inputProvider;
233  Config m_config;
234  ChannelPtrVector m_channels;
235  ChannelImplManager *m_channelImplManager;
236  cv::Mat m_image;
237  timeval m_lastUpdate;
238 
239  mutable unsigned char *m_bgr;
240  mutable unsigned m_bgrSize;
241  };
242 
247 }
248 
249 
250 
251 #endif